package com.tidu.demo;

import com.tidu.config.DataSourceConfig;
import com.tidu.dataSource.ConfigLoader;
import com.tidu.dataSource.DataSourceFactory;
import com.tidu.monitor.ConsoleProgressMonitor;
import com.tidu.strategy.SingleTableSyncDisruptorStrategy;
import com.tidu.template.AbstractDataSyncTemplate;
import com.tidu.template.impl.DisruptorDataSyncTemplate;

import javax.sql.DataSource;

public class syncDemo {
    public static void main(String[] args) {

        // 加载配置
        /*DataSourceConfig sourceConfig = ConfigLoader.loadDataSourceConfig(
                "jdbc:mysql://localhost:3306/source_db?useSSL=false&useUnicode=true&characterEncoding=utf8",
                "root",
                "1234",
                "com.mysql.cj.jdbc.Driver");
        DataSourceConfig targetConfig = ConfigLoader.loadDataSourceConfig(
                "jdbc:mysql://localhost:3306/target_db?useSSL=false&useUnicode=true&characterEncoding=utf8",
                "root",
                "1234",
                "com.mysql.cj.jdbc.Driver");*/
        DataSourceConfig sourceConfig = ConfigLoader.loadDataSourceConfig(
                "jdbc:mysql://localhost:3306/source_db?" +
                        "useSSL=false&useUnicode=true&characterEncoding=utf8&" +
                        "rewriteBatchedStatements=true&useServerPrepStmts=false&" +
                        "cachePrepStmts=true&prepStmtCacheSize=1000&" +  // 增加预编译语句缓存
                        "useLocalSessionState=true&elideSetAutoCommits=true&" +
                        "alwaysSendSetIsolation=false&enableQueryTimeouts=false&" +
                        "useLocalTransactionState=true&rewriteBatchedStatements=true&" +
                        "useCursorFetch=true&defaultFetchSize=5000&" +  // 增加fetch size
                        "socketTimeout=60000&connectTimeout=10000&" +  // 设置超时
                        "tcpNoDelay=true&tcpKeepAlive=true&" +  // TCP优化
                        "useCompression=true",  // 使用压缩
                "root",
                "1234",
                "com.mysql.cj.jdbc.Driver");

        DataSourceConfig targetConfig = ConfigLoader.loadDataSourceConfig(
                "jdbc:mysql://localhost:3306/target_db?" +
                        "useSSL=false&useUnicode=true&characterEncoding=utf8&" +
                        "rewriteBatchedStatements=true&useServerPrepStmts=false&" +
                        "cachePrepStmts=true&prepStmtCacheSize=1000&" +
                        "useLocalSessionState=true&elideSetAutoCommits=true&" +
                        "alwaysSendSetIsolation=false&enableQueryTimeouts=false&" +
                        "useLocalTransactionState=true&rewriteBatchedStatements=true&" +
                        "useCursorFetch=true&defaultFetchSize=5000&" +
                        "socketTimeout=60000&connectTimeout=10000&" +
                        "tcpNoDelay=true&tcpKeepAlive=true&" +
                        "useCompression=true&" +
                        "rewriteBatchedStatements=true",
                "root",
                "1234",
                "com.mysql.cj.jdbc.Driver");


        // 创建数据源
        DataSource sourceDs = DataSourceFactory.createDataSource(sourceConfig);
        DataSource targetDs = DataSourceFactory.createDataSource(targetConfig);

        // 选择同步类型
//        SyncType syncType = SyncType.FULL; // 或 SyncType.INCREMENTAL

        // 设置策略
//        SingleTableSyncStrategy strategy = new SingleTableSyncStrategy("tb_demo", sourceDs, targetDs);
        SingleTableSyncDisruptorStrategy strategy = new SingleTableSyncDisruptorStrategy("tb_demo", sourceDs, targetDs);
//        MultiTableSyncStrategy strategy = new MultiTableSyncStrategy(List.of("tb_dept", "tb_emp", "tb_demo"), sourceDs, targetDs);
//        DataBaseSyncStrategy strategy = new DataBaseSyncStrategy(sourceDs, targetDs);
//        CustomSqlSyncStrategy strategy = new CustomSqlSyncStrategy(sourceDs, targetDs, "select * from tb_emp", "tb_emp");

        // 创建执行程序
//        AbstractDataSyncTemplate syncTemplate = new DataSyncTemplate(strategy);
        AbstractDataSyncTemplate syncTemplate = new DisruptorDataSyncTemplate(strategy);

        // 创建监视者
        ConsoleProgressMonitor monitor = new ConsoleProgressMonitor();
        // 添加监视者
        syncTemplate.addObserver(monitor);

        // 记录开始时间
        long startTime = System.currentTimeMillis();

        // 开始同步
        syncTemplate.execute();

        // 记录结束时间并计算耗时
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;

        // 打印运行时间
        System.out.print("同步任务运行时间: " + duration + " 毫秒，");
        System.out.println("即 " + (duration / 1000.0) + " 秒");
    }
}
